﻿/**********************************************************************
 * @file   FITKCGNSReader.h
 * @brief  CGNS 格式读取接口
 * @author liuzhonghua (liuzhonghuaszch@163.com)
 * @date   2024-09-19
 *********************************************************************/

#include "FITKCGNSIOAPI.h"
#include "FITK_Interface/FITKInterfaceIO/FITKAbstractIO.h"
#include <QVector>
#include <QMap>
#include <cgnslib.h>

namespace Interface {
    class FITKUnstructuredFluidMeshVTK;
    class FITKBoundaryMeshVTK;
}
namespace IO
{
    /**
     * @brief  点数据信息结构体
     * @author liuzhonghua (liuzhonghuaszch@163.com)
     * @date   2024-09-19
     */
    struct VertexDataInfo
    {
        double x = 0;
        double y = 0;
        double z = 0;
    };
    /**
     * @brief  单元数据信息结构体
     * @author liuzhonghua (liuzhonghuaszch@163.com)
     * @date   2024-09-20
     */
    struct CellDataInfo
    {
        CellDataInfo() {}
        CellDataInfo(int cellType, QList<int> ids, int cellId = -1)
        {
            type = cellType;
            Points = ids;
            cId = cellId;
        }
        int cId = 0;
        int type = 0;
        QList<int> Points;
    };
    /**
     * @brief  section数据
     * @author liuzhonghua (liuzhonghuaszch@163.com)
     * @date   2024-09-21
     */
    struct SectionDataStruct
    {
        QString name;
        QVector<int> ids;
    };

    struct BCDataStruct
    {
        QString name;
        int bcType = 0;
        int structType = 0;
        int dataType = 0;//1 点 2 单元
        QVector<int> ids;
        int range[2][3] = { 0 };
    };
    /**
     * @brief  存储基础信息
     * @author liuzhonghua (liuzhonghuaszch@163.com)
     * @date   2024-09-20
     */
    struct baseDataStruct
    {
        QVector<VertexDataInfo> m_verlist{};
        QVector<CellDataInfo> m_cellList{};
        QVector<SectionDataStruct> m_sections{};
        QVector<BCDataStruct> m_bcs{};
        QMap<QString, QVector<double>> m_pointScalars;
        QMap<QString, QVector<double>> m_cellScalars;
    };

    /**
     * @brief  CGNS读取
     * @author liuzhonghua (liuzhonghuaszch@163.com)
     * @date   2024-09-19
     */
    class FITKCGNSIOAPI FITKCGNSReader : public Interface::FITKAbstractIO
    {
    public:
        /**
         * @brief  构造函数
         * @author YanZhiHui (chanyuantiandao@126.com)
         * @date   2024-07-29
         */
        FITKCGNSReader() = default;
        /**
         * @brief  析构函数
         * @author YanZhiHui (chanyuantiandao@126.com)
         * @date   2024-07-29
         */
        ~FITKCGNSReader();
        /**
         * @brief 重写run函数
         * @author fulipeng (fulipengqd@yeah.net)
         * @date 2024-03-11
         */
        void run() override;

        /**
         * @brief 打印控制台消息
         * @param[i] level 打印级别 1 normal 2 warning 3error
         * @param[i] str 数据
         * @author fulipeng (fulipengqd@yeah.net)
         * @date 2024-04-07
         */
        void consoleMessage(int level, const QString& str) override;

    private:
        bool read();
        bool readZone();
        bool readCoordinate(int vertext_num, /*int*/cgsize_t range_from[3], /*int*/cgsize_t range_to[3]);
        bool readZoneFlowSolution(int pointLen, int cellLen, cgsize_t* rmax_pt, cgsize_t* rmax_cell);

        bool readBoundaryCond(int index_bc);
        bool readZoneBCs(BCDataStruct* bcData, int index_bc, int cellCount);
        bool readUnstructZoneBCs(BCDataStruct* bcData, int index_bc);
        bool readStructZoneBCs(BCDataStruct* bcData, int index_bc);
        bool readSections();

        
        void readCells(SectionDataStruct* section,int start_index, int end_index, int type, cgsize_t* elements /*int* elements*/, int element_data_size);

        void setReadData();
    private:
        /**
         * @brief  网格数据对象
         * @author liuzhonghua (liuzhonghuaszch@163.com)
         * @date   2024-09-19
         */
        Interface::FITKUnstructuredFluidMeshVTK* m_Mesh{};
        int m_index_file{ -1 };
        int m_index_base{ -1 };
        int m_index_zone{ -1 };

        QList<baseDataStruct*> m_baseDatas{};
        
    };

}